<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/jsp/common/headMain.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<title>商家管理</title>
  </head>
		<link rel="stylesheet" media="all" type="text/css" href="${base }/js/plugins/timepicker/css/jquery-ui-timepicker-addon.css"/>  
		<script type="text/javascript" src="${base }/js/plugins/timepicker/jquery-ui-timepicker-addon.js"></script>  
		<script type="text/javascript" src="${base }/js/plugins/timepicker/jquery-ui-sliderAccess.js"></script>  
		<script type="text/javascript" src="${base }/js/plugins/timepicker/jquery-ui-timepicker-zh-CN.js"></script>
  		<script type="text/javascript">
  		function _addShop() {
  			_clear();
			$("#_addDIV").dialog({
				height:400,width:500,resizable:false,modal:true, //这里就是控制弹出为模态                
				buttons: {
					"确定":function() {
						$.post("${base}/shop_addShop", 
							$('#_addForm').serialize(), 
							function(response) {
								if (response && response.success) {
									alert(response.msg);
									$("#_shopGrid").jqGrid('setGridParam',{url: '${base}/shop_getList'}).trigger("reloadGrid");
								} else {
									alert(response.msg);
								}
						}, "json");
						$(this).dialog("close");
					},                    
					"取消":function() {
						$(this).dialog("close");
					}                
				}            
			});
		}
		
		function _delShop(id) {
			if (confirm("删除商家吗？")) {
				$.post("${base}/shop_deleteShop/" + id, 
					function(data) {
						if (data && data.success) {
							alert(data.msg);
							$("#_shopGrid").jqGrid('setGridParam',{url: '${base}/shop_getList'}).trigger("reloadGrid");
						} else {
							alert(data.msg);
						}
					}, 
				"json");
			}
		}
		
		function _updateShop(id) { 
			_clear();
  			//单击修改链接的操作
  			var model = $("#_shopGrid").jqGrid('getRowData', id);
			$("#m_shopname").val($("#span_view_" + id).html());
			$("#m_linkman").val(model.linkman);
			$("#m_linkmanphone").val(model.linkmanphone);
			$("#m_shopaddress").val(model.shopaddress);
			$("#m_keyworde").val(model.keyworde);
			$("#m_creator").val(model.creator);
			$("#m_creatorname").val(model.creatorname);
			$("#m_creatertime").val(model.creatertime);
			
			$("#m_addressID").val(model.address_id);
			$("#m_address_name").val(model.address_name);
			$("#parentid").val(model.parentid);
			
			$("input[id='m_status'][value='" + model.status + "']").attr("checked", true);
			$("input[id='m_source_from'][value='" + model.source_from + "']").attr("checked", true);
			
			$("#m_shopdesc").val(model.shopdesc);
			$("#m_idcard").val(model.idcard);
			$("input[id='m_isordertime'][value='" + model.isordertime + "']").attr("checked", true);
			if (model.isordertime == 1) {
				$("#mp_order_begintime").show();
				$("#mp_order_endtime").show();
				$("#m_order_begintime").val(model.order_begintime);
				$("#m_order_endtime").val(model.order_endtime);
			} else {
				$("#m_order_begintime").val("");
				$("#m_order_endtime").val("");
				$("#mp_order_begintime").hide();
				$("#mp_order_endtime").hide();
			}
			$("input[id='m_isadminonline'][value='" + model.isadminonline + "']").attr("checked", true);
			$("input[id='m_islowestcost'][value='" + model.islowestcost + "']").attr("checked", true);
			if (model.islowestcost == 1) {
				$("#mp_lowestcost").show();
				$("#m_lowestcost").val(model.lowestcost);
			} else {
				$("#m_lowestcost").val("0");
				$("#mp_lowestcost").hide();
			}
			
			$("#_updateDIV").dialog({
				height:470,width:500,resizable:false,modal:true, //这里就是控制弹出为模态                
				buttons: {
					"确定":function() {
						$.post("${base}/shop_updateShop/" + id, 
							$('#_updateForm').serialize(), 
							function(response) {
								if (response && response.success) {
									alert(response.msg);
									$("#_shopGrid").jqGrid('setGridParam',{url: '${base}/shop_getList'}).trigger("reloadGrid");
								} else {
									alert(response.msg);
								}
						}, "json");
						$(this).dialog("close");
					},                    
					"取消":function() {
						$(this).dialog("close");
					}                
				}            
			});
		}
		
		function _viewShop(id) {
			_clear();
  			var model = $("#_shopGrid").jqGrid('getRowData', id);
			$("#v_shopname").val($("#span_view_" + id).html());
			$("#v_linkman").val(model.linkman);
			$("#v_linkmanphone").val(model.linkmanphone);
			$("#v_shopaddress").val(model.shopaddress);
			$("#v_creatorname").val(model.creatorname);
			$("#v_creatertime").val(model.creatertime);
			$("#v_addr").val(model.address_name);
			switch (model.status) {
				case '0': $("#v_status").html("未通过"); break;
				case '1': $("#v_status").html("已通过"); break;
				case '2': $("#v_status").html("审批中"); break;
			}
			switch (model.source_from) {
				case '1': $("#v_source_from").html("用户申请"); break;
				case '2': $("#v_source_from").html("管理员添加"); break;
			}
			$("#v_shopdesc").val(model.shopdesc);
			$("#v_idcard").val(model.idcard);
			if (model.isordertime == 1) {
				$("#v_isordertime").html("是");
				$("#vp_order_begintime").show();
				$("#vp_order_endtime").show();
				$("#v_order_begintime").val(model.order_begintime);
				$("#v_order_endtime").val(model.order_endtime);
			} else {
				$("#v_isordertime").html("否");
				$("#v_order_begintime").val("");
				$("#v_order_endtime").val("");
				$("#vp_order_begintime").hide();
				$("#vp_order_endtime").hide();
			}
			$("#v_isadminonline").html((model.isadminonline == 1) ? "是" : "否");
			if (model.v_islowestcost == 1) {
				$("#v_islowestcost").html("是");
				$("#vp_lowestcost").show();
				$("#v_lowestcost").val(model.lowestcost);
			} else {
				$("#v_islowestcost").html("否");
				$("#v_lowestcost").val("0");
				$("#vp_lowestcost").hide();
			}
			
			$("#_viewDIV").dialog({
				height:470,width:500,resizable:false,modal:true, //这里就是控制弹出为模态                
				buttons: {
					"确定":function() {
						$(this).dialog("close");
					}             
				}
			});
		}
		
		function _changeShopStatus(id, status, creator) {
			$.post("${base}/shop_changeShopStatus/" + id + "," + status + "," + creator, 
					function(data) {
						if (data && data.success) {
							alert(data.msg);
							$("#_shopGrid").jqGrid('setGridParam',{url: '${base}/shop_getList'}).trigger("reloadGrid");
						} else {
							alert(data.msg);
						}
					}, 
			"json");
		}
  		
		$(function(){
				$("#_shopGrid").jqGrid({
				   	url: "${base}/shop_getList",
					datatype: "json",
				   	colNames:['ID','地址', '关键字', '商家名称','食品管理','订单管理','联系人','联系人电话', '创建人ID', 
				   			  '创建人名称', '创建时间', '状态', '商家来源', '商家简介', '身份证', '是否启用订餐时间', 
				   			  '点餐开始时间', '点餐结束时间', '管理员是否在线', '是否启用最低消费', '最低消费金额', '操作', '地址ID', '地址名称', '地址父ID'],
				   	colModel:[
				   		{name:'id',index:'id', width:55, align:'center',hidden:true},
				   		{name:'shopaddress', index:'shopaddress', hidden:true},
				   		{name:'keyworde', index:'keyworde', hidden:true},
				   		{name:'shopname', width:300, index:'shopname', align:'center'},
				   		{name:'foodmanager', index:'foodmanager', align:'center'},
				   		{name:'showorders', index:'showorders', align:'center'},
				   		{name:'linkman', index:'linkman', align:'center'},
				   		{name:'linkmanphone', index:'linkmanphone', align:'center'},
				   		{name:'creator', index:'creator', hidden:true},
				   		{name:'creatorname', index:'creatorname', hidden:true},
				   		{name:'creatertime', index:'creatertime', hidden:true},
				   		{name:'status', index:'status', editable: false, formatter:'select', editoptions:{value:"0:不通过;1:已通过;2:审批中"}, hidden:true},
				   		{name:'source_from', index:'source_from', editable: false, formatter:'select', editoptions:{value:"1:用户申请;2:管理员添加"}, hidden:true},
				   		
				   		{name:'shopdesc',index:'shopdesc', align:'center',hidden:true},
				   		{name:'idcard',index:'idcard', align:'center',hidden:true},
				   		{name:'isordertime',index:'isordertime', align:'center',hidden:true},
				   		{name:'order_begintime',index:'order_begintime', align:'center',hidden:true},
				   		{name:'order_endtime',index:'order_endtime', align:'center',hidden:true},
				   		{name:'isadminonline',index:'isadminonline', align:'center',hidden:true},
				   		{name:'islowestcost',index:'islowestcost', align:'center',hidden:true},
				   		{name:'lowestcost',index:'lowestcost', align:'center',hidden:true},
				   		
				   		{name:'ctrl', index:'ctrl', align:'center', sortable:false},
				   		{name:'address_id', index:'address_id', hidden:true},
				   		{name:'address_name', index:'address_name', hidden:true},
				   		{name:'parentid', index:'parentid', hidden:true}
				   	],
				   	width:_width_,
				   	height:230,
				   	rowNum:10,
				   	rowList:[10,20,30],
				   	pager: '#_shopBar',
				   	sortname: 'id',
				    viewrecords: true,
				    sortorder: "desc",
					multiselect: false,
					jsonReader: {
						repeatitems : false
					},
					rownumbers : true,
					caption: "商家管理",
					afterInsertRow : function(rowid, data) {
						var status_dis = "";
						switch (data.status) {
							case 0: status_dis = "未通过"; break;
							case 1: status_dis = "已通过"; break;
							case 2: status_dis = "审批中"; break;
						}
						$("#_shopGrid").jqGrid('setCell', rowid, 'shopname', '<span style="cursor:pointer;" onclick="_viewShop(' + data.id + ');" id="span_view_'+ data.id +'">' + data.shopname + '</span>', {color:'green'});
						$("#_shopGrid").jqGrid('setCell', rowid, 'foodmanager', '<span style="cursor:pointer;color:green;" onclick="_foodManager(' + data.id + ');">食品管理</span>', {color : 'purple'});
						$("#_shopGrid").jqGrid('setCell', rowid, 'showorders', '<span style="cursor:pointer;color:green;" onclick="_orderManager(' + data.id + ');">订单管理</span>', {color : 'purple'});
						$("#_shopGrid").jqGrid('setCell', rowid, 'ctrl', '<span style="cursor:pointer;" onclick="_updateShop(' + data.id + ');">修改</span>|<span style="cursor:pointer;" onclick="_delShop(' + data.id + ');">删除</span>|<span style="cursor:pointer;" onclick="_changeShopStatus(' + data.id + ',' + data.status + ',' + data.creator + ');">' + status_dis + '</span>', {color:'green'});
					}
				});
				$("#_shopGrid").jqGrid('navGrid','#_shopBar',{add : false, del : false, edit : false, refresh : true, search : true});
		});
		
		function _foodManager(shopId) {//食物管理
			window.location.href = "${base}/food_foodManagerList/" + shopId;
		}
		
		function _orderManager(shopId) {//订单管理
			window.location.href = "${base}/order_orderManagerList/" + shopId;
		}
		
		function _show(obj, type) {
			var val = obj.value;
			switch (type) {
				case '1': 
						if (val == '1') {
							$("#mp_order_begintime").show();
							$("#mp_order_endtime").show();
						} else {
							$("#m_order_begintime").val("");
							$("#m_order_endtime").val("");
							$("#mp_order_begintime").hide();
							$("#mp_order_endtime").hide();
						}
						break;
				case '2':
						if (val == '1') {
							$("#mp_lowestcost").show();
						} else {
							$("#m_lowestcost").val("0");
							$("#mp_lowestcost").hide();
						}
						break;
			}
		}
		
		$(document).ready(function() {
			$("#m_creatertime").datetimepicker({
						showSecond: true,   
			            timeFormat: 'hh:mm:ss',   
			            stepHour: 1,   
			            stepMinute: 5,   
			            stepSecond: 10
			});
			if ($("#m_order_begintime").show()) {
				$("#m_order_begintime").datetimepicker({
						showSecond: true,   
			            timeFormat: 'hh:mm:ss',   
			            stepHour: 1,   
			            stepMinute: 5,   
			            stepSecond: 10  
				});
			}
			if ($("#m_order_endtime").show()) {
				$("#m_order_endtime").datetimepicker({
						showSecond: true,   
			            timeFormat: 'hh:mm:ss',   
			            stepHour: 1,   
			            stepMinute: 5,   
			            stepSecond: 10  
				});
			}
			jQuery.div.util.dialogInit("地区选择", "_addressTreeRD", 100,400);
			$("#btn_query").click(function() {
			    $("#_shopGrid").jqGrid('setGridParam',{
					url:"${base}/shop_getList",
					postData:{'qShopName':$("#q_shopName").val(),'qLinkMan':$("#q_linkMan").val()},
					page:1
				}).trigger("reloadGrid");
			});
		});
		
		function _chooseAddress(type) {
			$('#_addressTreeRD').dialog('open').load(jQuery.getBasePath() + '/address_enterShopAddressTree/' + type,function(){
				jQuery.div.util.hideLoading();
			});
		}
		
		function _clear() {
			$("input[type='text']").each(function() {
				$(this).val("");
			});
		}
		</script>
	<body>
	<table cellpadding="0" cellspacing="0" border="0" width="99%" style="height:100%;">
		<tr><td style="height:10px; overflow:hidden;" colspan="2"></td></tr>
		<tr>
			<!--右边内容-->
			<td width="*" valign="top" class="contList">
				<div id="_shopManagerDiv">
					<!-- 显示商家列表 
						<table id="_shopGrid"></table>
						<div id="_shopBar"></div>
						<div class="contBtn" style="width:900px; _width:902px;"> width 和id="_UserGrid"宽度设置一致;IE宽度需多加左右border的宽度
							<input type="button" value="增加商家" class="btn" onclick="_addShop();"/>
						</div>
					--> 	
				    <!-- begin -->
					<div class="mainInfo">
						<div class="subScroll" id="subScroll"><!-- 滚动条 -->
							<form action="" name="_queryForm" method="post">
							<div class="searchList">
								<ul>
									<li><span>商家名称：</span>
										<input type="text" id="q_shopName" name="q_shopName" />
									</li>
									<li><span>联系人：</span>
										<input type="text" id="q_linkMan" name="q_linkMan" />
									</li>
								</ul>
								<div class="clearfloat"></div>
								<div style="text-align: center;">
									<strong><em class="searchIcon"></em><input type="button" value="查询" class="btn" id="btn_query" /></strong>
									<strong><em class="clearIcon"></em><input type="button" value="清空查询" class="btn" onclick="javascript:_clear();" /></strong>
								</div>
								<div class="searchListBg"></div>
							</div>
							</form>
							<div class="mainTable">
								<table id="_shopGrid"></table><!-- 显示列表内容 -->
								<div id="_shopBar"></div>
								<div class="contBtn btn">
									<strong><em class="addIcon"></em><input type="button" value="增加商家" class="btn" onclick="_addShop();"/></strong>
								</div>
									<div class="minwidth_div"></div>
								</div>
							</div>
						</div>
					</div>
					<!-- end  -->
						
					<input id="parentid" type="hidden" value="-1" /><!-- 选中节点的id -->
					<div id="_addressTreeRD"></div><!--地址数 单选页面-->	
						
					<div id="_addDIV" title="增加商家" style="display:none;">
						<form id="_addForm" style="margin-top: 20px;padding:2px;">
							<input type="hidden" name="creator" />
							<input type="hidden" name="source_from" value="2" />
							<input type="hidden" name="status" value="1" />
							<p>商家名称：<input type="text" id="a_shopname" name="shopname" /></p>
							<p>联系人：  <input type="text" id="a_linkman" name="linkman" /></p>
							<p>联系人电话：<input type="text" id="a_linkmanphone" name="linkmanphone" /></p>
							<p>商家所在地区：<input type="text" id="a_addressNAME" name="placename" readonly="readonly" />
							<input type="hidden" id="a_addressID" name="addressid" />
							<input type="button" value="选择" onclick="_chooseAddress('0');" />
							</p>
							<p>详细地址：<input type="text" id="a_shopaddress" name="shopaddress" /></p>
							<p>关键字 ：	<input type="text" id="a_keyworde" name="keyworde" /></p>
						</form>
					</div>
					<div id="_updateDIV" title="修改商家" style="display:none;">
						<form id="_updateForm" style="margin-top: 20px;padding:2px;"> 
							<input type="hidden" name="creator" id="m_creator" />
							<p>商家名称：<input type="text" id="m_shopname" name="shopname" /></p>
							<p>联系人：  <input type="text" id="m_linkman" name="linkman" /></p>
							<p>联系人电话：<input type="text" id="m_linkmanphone" name="linkmanphone" /></p>
							<p>商家所在地区：<input type="text" id="m_address_name" name="placename" />
							<input type="hidden" id="m_addressID" name="addressid" />
							<input type="button" value="选择" onclick="_chooseAddress('1');" />
							</p>
							<p>详细地址：<input type="text" id="m_shopaddress" name="shopaddress" /></p>
							
							<p>关键字 ：	<input type="text" id="m_keyworde" name="keyworde" /></p>
							<p>创建人 ：	<input type="text" id="m_creatorname" /></p>
							<p>创建时间 ：<input type="text" id="m_creatertime" name="creatertime" /></p>
							<p>
								状态 ：
									<input type="radio" id="m_status" name="status" value="0" />审批不通过
									<input type="radio" id="m_status" name="status" value="1" />审批通过
									<input type="radio" id="m_status" name="status" value="2" />审批中
							</p>
							<p>商家来源 ：<input type="radio" id="m_source_from" name="source_from" value="1" />用户申请<input type="radio" id="m_source_from" name="source_from" value="2" />管理员添加</p>
							<p>商家介绍 ：<textarea id="m_shopdesc" name="shopdesc"></textarea></p>
							<p>身份证 ：<input id="m_idcard" name="idcard" type="text" /></p>
							<p>是否启用订餐时间 ：
							<input id="m_isordertime" name="isordertime" type="radio" value="1" onclick="javascript:_show(this, '1');" />是
							<input id="m_isordertime" name="isordertime" type="radio" value="0" onclick="javascript:_show(this, '1');" />否
							</p>
							<p id="mp_order_begintime">订餐开始时间 ：<input id="m_order_begintime" name="order_begintime" type="text" /></p>
							<p id="mp_order_endtime">订餐结束时间 ：<input id="m_order_endtime" name="order_endtime" type="text" /></p>
							<p>管理员是否在线 ：<input id="m_isadminonline" name="isadminonline" type="radio" value="1" />是<input id="m_isadminonline" name="isadminonline" type="radio" value="0" />否</p>
							<p>是否启用最低消费 ：
							<input id="m_islowestcost" name="islowestcost" type="radio" value="1" onclick="javascript:_show(this, '2');" />是
							<input id="m_islowestcost" name="islowestcost" type="radio" value="0" onclick="javascript:_show(this, '2');" />否
							</p>
							<p id="mp_lowestcost">最低消费金额 ：<input id="m_lowestcost" name="lowestcost" type="text" value="0" /></p>
						</form>
					</div>
					<div id="_viewDIV" title="查看商家" style="display:none;">
						<p>商家名称：<input type="text" id="v_shopname" /></p>
						<p>联系人：  <input type="text" id="v_linkman" /></p>
						<p>联系人电话：<input type="text" id="v_linkmanphone" /></p>
						<p>商家所在地区：<input type="text" id="v_addr" /></p>
						<p>详细地址：<input type="text" id="v_shopaddress" /></p>
						<p>关键字 ：	<input type="text" id="v_keyworde" /></p>
						<p>创建人 ：	<input type="text" id="v_creatorname" /></p>
						<p>创建时间 ：<input type="text" id="v_creatertime" /></p>
						<p>状态 ：<span id="v_status"></span></p>
						<p>商家来源 ：<span id="v_source_from"></span></p>
						<p>商家介绍 ：<textarea id="v_shopdesc" name="shopdesc"></textarea></p>
						<p>身份证 ：<input id="v_idcard" name="idcard" type="text" /></p>
						<p>是否启用订餐时间 ：<span id="v_isordertime"></span></p>
						<p id="vp_order_begintime">订餐开始时间 ：<input id="v_order_begintime" name="order_begintime" type="text" /></p>
						<p id="vp_order_endtime">订餐结束时间 ：<input id="v_order_endtime" name="order_endtime" type="text" /></p>
						<p>管理员是否在线 ：<span id="v_isadminonline"></span></p>
						<p>是否启用最低消费 ：<span id="v_islowestcost"></span></p>
						<p id="vp_lowestcost">最低消费金额 ：<input id="v_lowestcost" name="lowestcost" type="text" /></p>
					</div>
			</td>
	</table>
	</body>
</html>